package com.ug.system.processor;

import com.ug.common.cache.redis.RedisCacheService;
import com.ug.common.constant.HotConstants;
import com.ug.common.utils.SpringContextHolder;
import com.ug.system.entity.AttributeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.selector.XpathSelector;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: LY
 * @description: 豆瓣一周口碑榜爬取
 */
public class DoubanHotHtmlPageProcessor implements PageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(DoubanHotHtmlPageProcessor.class);
    private static final RedisCacheService redisCacheService = SpringContextHolder.getBean(RedisCacheService.class);
    //  部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private final Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        logger.info("获取豆瓣一周口碑榜数据");
        String key = HotConstants.HOT_TYPE_DOUBAN.concat(HotConstants.HOT_TYPE_KEY);
        try {
            //  获取html页面元素下的指定元素
            Selectable select = page.getHtml().$("#listCont2").select(
                    new XpathSelector("li[@class='clearfix']")
            );
            List<AttributeValue> attributeValues = new ArrayList<>();
            //  获取到的多个元素
            List<Selectable> nodes = select.nodes();
            //  循环获取对应标签数据
            for (Selectable node : nodes) {
                //  获取链接
                String link = node.$("a", "href").get();
                //  获取标题
                String title = node.$("a", "text").get();
                //  获取热搜指数
                String value = "0";
                AttributeValue attributeValue = new AttributeValue(title, link, value);
                attributeValues.add(attributeValue);
            }
            redisCacheService.put(key, attributeValues, HotConstants.HOT_TIME_OUT, TimeUnit.HOURS);
            page.putField("attributeValues", attributeValues);
        }catch (Exception e){
            logger.error("获取豆瓣一周口碑榜数据异常：{}", e.getMessage());
            page.putField("attributeValues", new ArrayList<>());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

}
